package il.ac.braude.sona.control;

import il.ac.braude.sona.database.ChangeRequest;
import il.ac.braude.sona.infogui.ChangeRequestInfoFrame;
import il.ac.braude.sona.ocsf.Mssg;
import il.ac.braude.sona.ocsf.P;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;
import il.ac.braude.sona.ocsf.server.DatabaseController;
import il.ac.braude.sona.strings.Resources;
import il.ac.braude.sona.table.ChangeRequestTableFrame;
import il.ac.braude.sona.table.ChangeRequestTableModel;
import il.ac.braude.sona.ui.forms.FormFrame;
import il.ac.braude.sona.ui.forms.FormListener;
import il.ac.braude.sona.ui.forms.myforms.CreateChangeRequestForm;
import il.ac.braude.sona.ui.forms.myforms.ViewChangeRequestForm;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JOptionPane;
import javax.swing.JTable;

public class ChangeRequestController implements  FormListener {
	private CreateChangeRequestForm reqForm;
	private ViewChangeRequestForm viewReqForm;

	public void execCreateChangeRequest() {
		if (reqForm==null) {
			reqForm  = new CreateChangeRequestForm();
			reqForm.addFormListener(ChangeRequestController.this);
			reqForm.showFrame();
		}
		else reqForm.toFront();
	}

	private void viewReq(String id) {
		try {
			HashMap<String, String> hash = new HashMap<String,String>();
			hash.put("key", "change request by id");
			hash.put("value", id);
			Mssg mssg = new Mssg(P.view,hash);
			viewReqForm.showMessege(Resources.Strings.Connecting);
			Client.getInstance().sendToServer(new ServerResponseListener() {
				
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.equals(P.error)) {
						viewReqForm.showMessege((String) mssg.getMessege());
						return;
					}
					ChangeRequest req;
					if ((req=(ChangeRequest) mssg.getMessege())!=null)
							new ChangeRequestInfoFrame(req);
					else JOptionPane.showMessageDialog(viewReqForm,String.format(Resources.Strings.NoSuchRequest));
					viewReqForm.dispose();
					viewReqForm=null;
				}
			},mssg);

		} catch (Exception e) {
			e.printStackTrace();
			viewReqForm.dispose();
			viewReqForm=null;
		} 
	}
	private void onCreateReqSubmitted(ArrayList<String> values) {
		try {
			Mssg msg = new Mssg(P.create,values);
			reqForm.showMessege(Resources.Strings.Connecting);
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.equals(P.error)) {
						reqForm.showMessege((String) mssg.getMessege());
						return;
					}
					long id = (Long) mssg.getMessege();
					JOptionPane.showMessageDialog(reqForm,String.format(Resources.Strings.RequestApproveID, ""+id));
					reqForm.dispose();
					reqForm=null;
				}
			}, msg);
			
		} catch (Exception e) {
			e.printStackTrace();
			reqForm.showError("Something went wrong with the database.");
			reqForm.dispose();
			reqForm=null;
		}
	}
	public void execViewChangeRequest() {
		if (viewReqForm==null) {
			viewReqForm  = new ViewChangeRequestForm();
			viewReqForm.addFormListener(ChangeRequestController.this);
			viewReqForm.showFrame();
		}
		else viewReqForm.toFront();
	}
	
	//Callbacks//
	
	@Override
	public void onFormCanceled(FormFrame formFrame) {
		if (formFrame==viewReqForm) {
			viewReqForm.dispose();
			viewReqForm=null;
		}
		else if (formFrame==reqForm) {
			reqForm.dispose();
			reqForm=null;
		}
	}
	
	@Override
	public void onFormSubmited(FormFrame formFrame, ArrayList<String> values) {
		if (formFrame==reqForm) onCreateReqSubmitted(values);
		else if (formFrame==viewReqForm) viewReq(values.get(0));
	}

	public void execViewAllChangeRequests() {
		try {
			HashMap<String, String> hash = new HashMap<String,String>();
			hash.put("key", "view all requests");
			Mssg msg = new Mssg(P.view,hash);
			final ChangeRequestTableFrame tableFrame = new ChangeRequestTableFrame();
			tableFrame.showFrame();
			Client.getInstance().sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.equals(P.error)) {
						JOptionPane.showMessageDialog(null,((String) mssg.getMessege()));
						return;
					}
					final JTable table = new JTable(new ChangeRequestTableModel((ArrayList<ChangeRequest>) mssg.getMessege()));
					table.addMouseListener(new java.awt.event.MouseAdapter() {
					    @Override
					    public void mouseClicked(java.awt.event.MouseEvent evt) {
					    	if (evt.getClickCount()<2) return;
					        int row = table.rowAtPoint(evt.getPoint());
					        int col = table.columnAtPoint(evt.getPoint());
					        if (row >= 0 && col >= 0) 
					        	new ChangeRequestInfoFrame(((ChangeRequestTableModel)table.getModel()).getChangeRequest(row));
					    }
					});
					tableFrame.setTable(table);
				}
			}, msg);
			
		} catch (IOException e) {
			JOptionPane.showMessageDialog(reqForm,Resources.Strings.ConnectFailed);
		}
	}
	

}
